/*
 *
 * System2 Simulation Framework
 * Copyright (C) 2011 - 2012 Jacob Dawid
 * jacob.dawid@googlemail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "signalbundle.h"

namespace System2 {
    SignalBundle::SignalBundle()
        : Serializable() {
    }

    SignalBundle::~SignalBundle() {
        foreach(Signal* signal, m_signals) {
            delete signal;
        }
    }

    SignalBundle::SignalBundle(const SignalBundle &other) {
        QList<int> channels = other.m_signals.keys();
        foreach(int channel, channels) {
            Signal *signal = other.m_signals[channel];
            m_signals[channel] = Signal::copy(signal);
        }
    }

    QString SignalBundle::serialize() {
        QString result = "<SignalBundle>\n";
        QList<int> _usedChannels = usedChannels();
        foreach(int channel, _usedChannels) {
            result += QString("<Signal channel=\"%1\">\n").arg(channel);
            result += m_signals[channel]->serialize();
            result += "</Signal>\n";
        }

        return result + "</SignalBundle>\n";
    }

    bool SignalBundle::unserialize(const QString& data) {
        (void)data;
        return true;
    }

    int SignalBundle::numberOfChannels() {
        return m_signals.size();
    }

    QList<int> SignalBundle::usedChannels() const {
        return m_signals.keys();
    }

    void SignalBundle::eraseChannel(int channel) {
        if(m_signals.contains(channel)) {
            delete m_signals[channel];
            m_signals.remove(channel);
        }
    }

    void SignalBundle::setSignal(int channel, Signal* newSignal) {
        Signal *existingSignal = signal(channel);
        delete existingSignal;
        m_signals[channel] = newSignal;
    }

    Signal *SignalBundle::signal(int channel) {
        if(m_signals.contains(channel)) {
            return m_signals[channel];
        }
        return 0;
    }
}
